package com.lindroy.viewpagerfragmentpractice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.lindroy.viewpagerfragmentpractice.fragment.AFragment
import com.lindroy.viewpagerfragmentpractice.fragment.BFragment
import com.lindroy.viewpagerfragmentpractice.fragment.BaseFragment
import kotlinx.android.synthetic.main.activity_sample.*

class SampleActivity : AppCompatActivity() {

    private val fragments = arrayListOf<BaseFragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sample)

        if(fragments.isNotEmpty()){
            fragments.clear()
        }
        fragments.add(AFragment())
        fragments.add(BFragment())

        viewPager.adapter = object :FragmentPagerAdapter(supportFragmentManager){

            override fun getCount() = fragments.size

            override fun getItem(position: Int): Fragment  = fragments[position]
        }

        button.setOnClickListener {
            Log.d("Tag","curFragment = ${fragments[viewPager.currentItem]}")
            fragments[viewPager.currentItem].showMessage("hello")
        }
    }
}